package o;

import android.text.TextWatcher;
import android.text.style.CharacterStyle;
import android.util.Pair;
import android.widget.EditText;

public class btw {
    private static Pair<Integer, Integer> ˋ(EditText editText, int i) {
        int i2 = 0;
        int selectionEnd = editText.getSelectionEnd();
        CharacterStyle[] characterStyleArr = (CharacterStyle[]) editText.getText().getSpans(0, editText.getText().toString().length(), CharacterStyle.class);
        int length = characterStyleArr.length;
        while (i2 < length) {
            Object obj = characterStyleArr[i2];
            if (!(obj == null || (obj instanceof btv))) {
                int spanStart = editText.getText().getSpanStart(obj);
                int spanEnd = editText.getText().getSpanEnd(obj);
                if (ॱ(i, selectionEnd, spanStart, spanEnd)) {
                    return new Pair(Integer.valueOf(spanStart), Integer.valueOf(spanEnd));
                }
                if (spanStart > selectionEnd) {
                    return null;
                }
            }
            i2++;
        }
        return null;
    }

    private static boolean ॱ(int i, int i2, int i3, int i4) {
        return (i3 <= i && i2 <= i4) || ((i3 <= i && i <= i4) || ((i3 <= i2 && i2 <= i4) || (i <= i3 && i4 <= i2)));
    }

    public static boolean ॱ(EditText editText, int i) {
        Pair ˋ = ˋ(editText, i);
        if (ˋ == null || !ˏ(i, ((Integer) ˋ.first).intValue(), ((Integer) ˋ.second).intValue())) {
            return false;
        }
        return true;
    }

    private static boolean ˏ(int i, int i2, int i3) {
        return i2 < i && i < i3;
    }

    public static void ˋ(int i, int i2, EditText editText) {
        Pair ˋ = ˋ(editText, i);
        if (ˋ != null) {
            if (i != i2) {
                int intValue = ((Integer) ˋ.first).intValue();
                int intValue2 = ((Integer) ˋ.second).intValue();
                if (i == ((Integer) ˋ.second).intValue()) {
                    intValue = i;
                }
                ˋ = ˋ(editText, i2);
                int intValue3 = ˋ != null ? i2 == ((Integer) ˋ.first).intValue() ? i2 : ((Integer) ˋ.second).intValue() : intValue2;
                editText.setSelection(Math.min(intValue, i), Math.max(intValue3, i2));
            } else if (i != ((Integer) ˋ.first).intValue()) {
                editText.setSelection(((Integer) ˋ.second).intValue());
            }
        }
    }

    public static void ˊ(int i, TextWatcher textWatcher, EditText editText) {
        Pair ˋ = ˋ(editText, i);
        if (ˋ != null) {
            editText.removeTextChangedListener(textWatcher);
            editText.getEditableText().delete(((Integer) ˋ.first).intValue(), ((Integer) ˋ.second).intValue());
            editText.addTextChangedListener(textWatcher);
        }
    }
}
